package com.ruicar.afs.cloud.image.util;

import cn.hutool.core.img.Img;
import com.ruicar.afs.cloud.image.entity.ImgDrawStringEntity;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;

/**
 * @Description:
 * @Author: fangchenliang
 * @Date: 2020/8/26 19:00
 */
@Component
public class DrawImg {
    /**
     * 给图片添加文字水印<br>
     * 此方法并不关闭流
     * 默认居中 x,y表示居中的偏移
     */
    public BufferedImage drawStringOnImg(BufferedImage inputBufferedImage,List<ImgDrawStringEntity> list) {
        Graphics2D g = inputBufferedImage.createGraphics();
        // 抗锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        for(ImgDrawStringEntity entity:list){
            g.setColor(entity.getContentColor());
            g.setFont(entity.getFont());
            /**透明度*/
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, entity.getAlpha()));

            // 在指定坐标绘制水印文字
            final FontMetrics metrics = g.getFontMetrics(entity.getFont());
            final int textLength = metrics.stringWidth(entity.getContent());
            final int textHeight = metrics.getAscent() - metrics.getLeading() - metrics.getDescent();
            /**居中偏移度*/
            int x=entity.getX();
            int y=entity.getY();
            /**旋转角度*/
            if(entity.getRotate() != null) {
                g.rotate(entity.getRotate(),Math.abs(inputBufferedImage.getWidth() - textLength) / 2 + x, Math.abs(inputBufferedImage.getHeight() + textHeight) / 2 + y);
            }
            g.drawString(entity.getContent(), Math.abs(inputBufferedImage.getWidth() - textLength) / 2 + x, Math.abs(inputBufferedImage.getHeight() + textHeight) / 2 + y);
        }
        g.dispose();
        return inputBufferedImage;
    }

    public BufferedImage mergeImage(BufferedImage img1, BufferedImage img2,String title) {
        int w1 = img1.getWidth();
        int h1 = img1.getHeight();
        if(w1<h1){
            Img img=new Img(img1);
            img1=  img.rotate(270).getImg();
            w1 = img1.getWidth();
            h1 = img1.getHeight();
        }
        int w2 = img2.getWidth();
        int h2 = img2.getHeight();
        if(w2<h2){
            Img img=new Img(img2);
            img2=  img.rotate(270).getImg();
            w2=img2.getWidth();
            h2=img2.getHeight();
        }
        int w=0;
        if(w1>w2){
            w=w1;
        }else {
            w=w2;
        }
        Font font = new Font("宋体", Font.BOLD, 18);
        BufferedImage newImage = new BufferedImage(w+60, h2 + h1 + 200, BufferedImage.TYPE_INT_RGB);// 新的图
        int x=(newImage.getWidth()- w1)/ 2;
        Graphics2D graphics = (Graphics2D) newImage.getGraphics();
        graphics.setBackground(Color.WHITE);
        graphics.fillRect(0, 0, newImage.getWidth(), newImage.getHeight());
        graphics.drawImage(img1, (newImage.getWidth()- w1)/ 2, 20, null);
        graphics.drawImage(img2, (newImage.getWidth()- w2)/ 2, h1+100, null);
        graphics.setFont(font);
        graphics.setColor(Color.BLACK);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int width = graphics.getFontMetrics(font).stringWidth(title);
        int startY = h1 + 70;
        if (width > newImage.getWidth()) {
            char[] array = title.toCharArray();
            StringBuilder sb = new StringBuilder(array[0]);
            for (char c : array) {
                sb.append(c);
                int newWidth = graphics.getFontMetrics(font).stringWidth(sb.toString());
                if ((newWidth + 19) >= newImage.getWidth()) {// 准备换行
                    graphics.drawString(sb.toString(), (newImage.getWidth()- width)/ 2, startY);
                    startY += 30;
                    sb.delete(0, sb.length());
                }
            }
            graphics.drawString(sb.toString(), (newImage.getWidth()- width)/ 2, startY);
        } else {
            graphics.drawString(title, (newImage.getWidth()- width)/ 2, startY);
        }
        graphics.drawString("与原件保持一致", 50, h1+h2 + 150);
        return newImage;
    }
}
